﻿using QuanLyGiaoVuDAL.ADOWrapper;
using QuanLyGiaoVuDAL.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using QuanLyGiaoVuDAL.Mapper;

namespace QuanLyGiaoVuDAL.Repositories.DatabaseRepositories
{
    public class TienDoDeTaiRepository:ITienDoDeTaiRepository
    {
        public IEnumerable<TienDoDeTaiDTO> GetAllTienDoDeTai(Guid maDeTai)
        {
            List<TienDoDeTaiDTO> listTienDoDT = new List<TienDoDeTaiDTO>();
            SqlParameter[] parameters = new SqlParameter[1];
            parameters[0] = DBParameterFactory.Create("MaDeTai",maDeTai);
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_TienDoDeTaiSelect]", parameters);
            try
            {
                var caTable = dbResult.Tables[0].Rows;
                foreach (DataRow dataRow in caTable)
                {
                    listTienDoDT.Add(dataRow.ToTienDoDeTaiDTO());
                }
                return listTienDoDT.AsEnumerable();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public TienDoDeTaiDTO InsertTienDoDeTai(TienDoDeTaiDTO tienDoDTO)
        {
            var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_TienDoDeTaiInsert]", GetParameterArray(tienDoDTO));
            try
            {
                var dataRow = dbResult.Tables[0].Rows[0];
                return dataRow.ToTienDoDeTaiDTO();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public bool DeleteTienDoDeTai(Guid maTienDoDeTai)
        {
            SqlParameter[] parameters = new SqlParameter[2];
            parameters[0] = DBParameterFactory.Create("MaTienDoDeTai", maTienDoDeTai);
            parameters[1] = DBParameterFactory.Create("MaDeTai", SqlDbType.UniqueIdentifier, DBNull.Value);

            try
            {
                var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_TienDoDeTaiDelete]",parameters);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool DeleteAllTienDoDeTai(Guid maDeTai)
        {
            SqlParameter[] parameters = new SqlParameter[2];
            parameters[0] = DBParameterFactory.Create("MaTienDoDeTai", SqlDbType.UniqueIdentifier, DBNull.Value);
            parameters[1] = DBParameterFactory.Create("MaDeTai", maDeTai);

            try
            {
                var dbResult = StoreProcedureExecutor.ExecuteStoreProcedure("[dbo].[usp_TienDoDeTaiDelete]", parameters);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #region Private Methods
        private System.Data.SqlClient.SqlParameter[] GetParameterArray(TienDoDeTaiDTO tienDoDeTaiDTO)
        {
            SqlParameter[] parameters = new SqlParameter[6];
            parameters[0] = DBParameterFactory.Create("MaTienDoDeTai", tienDoDeTaiDTO.MaTienDoDeTai);
            parameters[1] = DBParameterFactory.Create("MaDeTai", tienDoDeTaiDTO.MaDeTai);
            parameters[2] = DBParameterFactory.Create("NoiDung", tienDoDeTaiDTO.NoiDung);
            parameters[3] = DBParameterFactory.Create("TieuDe", tienDoDeTaiDTO.TieuDe);
            parameters[4] = DBParameterFactory.Create("NgayBaoCao", tienDoDeTaiDTO.NgayBaoCao);
            parameters[5] = DBParameterFactory.Create("TienDoHoanThanh", tienDoDeTaiDTO.TienDoHoanThanh);
            return parameters;
        }
        #endregion
    }
}
